package com.sparrows.pirates.screens;

import com.sparrows.pirates.helpers.Animator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.sparrows.pirates.SparrowPiratesGame;

public class MainMenuScreen implements Screen {

	final SparrowPiratesGame game;
	
	Animator backgroundAnimation;
	Texture mainMenuTexMap;
	TextureRegion water, sand, title, ssg;
	
	public MainMenuScreen(final SparrowPiratesGame gam){
		game = gam;
		
		createTextures();
	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		//Update animation
		backgroundAnimation.update(delta);
		
		game.spriteBatch.setColor(1,1,1,1);
		game.spriteBatch.begin();
		
			//Background
			for (int i=0; i < 8; i++)
			{
				//Sand
				game.spriteBatch.draw(sand, 128*i, 492);
				//Animated Sand using Animator class
				backgroundAnimation.draw(game.spriteBatch, 128f*i, 364f);
				//Water
				for (int j = 0; j<3; j++){
				game.spriteBatch.draw(water, 128*i, 236 - 128*j);
				}
			}
			
			//Sophisticated Sparrow Gaming
			game.spriteBatch.draw(ssg, 353 , -33);
			
			//Title
			//Draw it black and transparent to make it seem "cut out" of the sand
			game.spriteBatch.setColor(0f, 0f, 0f, 0.5f);
			game.spriteBatch.draw(title, 289, 436);
		
		game.spriteBatch.end();
	}

	public void createTextures(){
		//Pull individual textures from texture map
		mainMenuTexMap = game.manager.get("textures/environment/textureSheet1.png");
		water = new TextureRegion(mainMenuTexMap, 0, 128, 128, 128);
		sand = new TextureRegion(mainMenuTexMap, 128, 128, 128, 128);
		title = new TextureRegion(mainMenuTexMap, 256, 128, 384, 128);
		ssg = new TextureRegion(mainMenuTexMap, 1152, 128, 256, 128);
		
		//Grab the 18 frames of the shore animation
		TextureRegion[][] tmp = TextureRegion.split(mainMenuTexMap,128,128);
		TextureRegion[] sandTmp = new TextureRegion[18];
		for (int i=0; i < 18; i++){
			sandTmp[i] = tmp[0][i];
		}
		
		//Animator class takes the frame rate, an array of texture regions, and whether or not it should loop
		backgroundAnimation = new Animator(0.09f, sandTmp, 18, true);
	}
	
	@Override
	public void resize(int width, int height) {
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
